package cn.iocoder.yudao.module.erp.controller.admin.exchange;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.exchange.vo.order.ErpExchangeOrderCreateReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.exchange.vo.order.ErpExchangeOrderPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.exchange.vo.order.ErpExchangeOrderRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.exchange.vo.order.ErpExchangeOrderUpdateReqVO;
import cn.iocoder.yudao.module.erp.convert.exchange.ErpExchangeOrderConvert;
import cn.iocoder.yudao.module.erp.dal.dataobject.exchange.ErpExchangeOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.exchange.ErpExchangeOrderItemDO;
import cn.iocoder.yudao.module.erp.service.exchange.ErpExchangeOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - ERP 旧料兑换订单")
@RestController
@RequestMapping("/erp/exchange-order")
@Validated
public class ErpExchangeOrderController {

    @Resource
    private ErpExchangeOrderService exchangeOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建旧料兑换订单")
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:create')")
    public CommonResult<Long> createExchangeOrder(@Valid @RequestBody ErpExchangeOrderCreateReqVO createReqVO) {
        return success(exchangeOrderService.createExchangeOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旧料兑换订单")
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:update')")
    public CommonResult<Boolean> updateExchangeOrder(@Valid @RequestBody ErpExchangeOrderUpdateReqVO updateReqVO) {
        exchangeOrderService.updateExchangeOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新旧料兑换订单的状态")
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:update')")
    public CommonResult<Boolean> updateExchangeOrderStatus(@RequestParam("id") Long id,
                                                        @RequestParam("status") Integer status) {
        exchangeOrderService.updateExchangeOrderStatus(id, status);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旧料兑换订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:delete')")
    public CommonResult<Boolean> deleteExchangeOrder(@RequestParam("id") Long id) {
        exchangeOrderService.deleteExchangeOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旧料兑换订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:query')")
    public CommonResult<ErpExchangeOrderRespVO> getExchangeOrder(@RequestParam("id") Long id) {
        ErpExchangeOrderDO exchangeOrder = exchangeOrderService.getExchangeOrder(id);
        if (exchangeOrder == null) {
            return success(null);
        }
        List<ErpExchangeOrderItemDO> exchangeOrderItems = exchangeOrderService.getExchangeOrderItemListByOrderId(id);
        return success(ErpExchangeOrderConvert.INSTANCE.convert(exchangeOrder, exchangeOrderItems));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旧料兑换订单分页")
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:query')")
    public CommonResult<PageResult<ErpExchangeOrderRespVO>> getExchangeOrderPage(@Valid ErpExchangeOrderPageReqVO pageVO) {
        PageResult<ErpExchangeOrderDO> pageResult = exchangeOrderService.getExchangeOrderPage(pageVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        // 获得项
        List<ErpExchangeOrderItemDO> exchangeOrderItems = exchangeOrderService.getExchangeOrderItemListByOrderIds(
                convertList(pageResult.getList(), ErpExchangeOrderDO::getId));
        return success(ErpExchangeOrderConvert.INSTANCE.convertPage(pageResult, exchangeOrderItems));
    }
    
    @PutMapping("/approve")
    @Operation(summary = "审核旧料兑换订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:update')")
    public CommonResult<Boolean> approveExchangeOrder(@RequestParam("id") Long id) {
        exchangeOrderService.approveExchangeOrder(id);
        return success(true);
    }
    
    @PutMapping("/disapprove")
    @Operation(summary = "反审核旧料兑换订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:exchange-order:update')")
    public CommonResult<Boolean> disapproveExchangeOrder(@RequestParam("id") Long id) {
        exchangeOrderService.disapproveExchangeOrder(id);
        return success(true);
    }

} 